$("#hub").append("<table style='color: black; z-index: 20; width: 85%; margin-left:55px; margin-top:5px; color:#FFF; position: absolute; font-family: verdana, sans-serif;'><tr><td id='points' style='width:33%'></td><td id='time' style='width:33%'></td><td id='playername'></td><td id='levelname' align='right'>Level 3</td></tr></table>");

gameTime = new Date().getTime();
var CONS_GAME_TIME = 1000*30;

function TargetDisk(node){
	this.node = $(node);
}
TargetDisk.prototype = new Enemy();
TargetDisk.prototype.damage = function(e){

	var textureWidthM = 18; //Texturebreite / 2
	var textureHeightm = 18; //Texturehöhe / 2
	
	var offset = this.node.offset();
	var pointX = e.pageX - offset.left;
	var pointY =  e.pageY - offset.top;

	//Frag Björn
	var strecke = Math.sqrt(Math.pow(textureWidthM - pointX, 2) + Math.pow(textureHeightm - pointY, 2));

	//Ziel wurde nicht getroffen
	if (strecke > 18)
		return false;

	if (strecke < 2)
		playerScore += 100;
	else if (strecke < 5)
		playerScore += 75;
	else if (strecke < 10)
		playerScore += 45;
	else
		playerScore += 5;

	this.shield--;
	if(this.shield == 0){
		return true;
	}
	return false;
};

TargetDisk.prototype.updateY = function(playerNode){
	var pos = parseInt(this.node.css("top"));
	if(pos > (PLAYGROUND_HEIGHT - 100)){
		this.node.css("top",""+(pos - 2)+"px");
	}
}

// The background:
var background = new $.gameQuery.Animation({imageURL: "data/textures/levels/level_3/background.png"});
var wall = new $.gameQuery.Animation({imageURL: "data/textures/levels/level_3/wall.png"});

//  List of enemies animations :
// 1st kind of enemy:
enemies[0] = new Array(); // enemies have two animations
enemies[0]["idle"]	= new $.gameQuery.Animation({imageURL: "data/textures/models/tutorial/tutorial.png"});
//enemies[0]["idle"]	= new $.gameQuery.Animation({imageURL: "/data/textures/models/enemy1/enemy_idle.png", numberOfFrame: 5, delta: 136, rate: 60, type: $.gameQuery.ANIMATION_VERTICAL});
//enemies[0]["explode"]	= new $.gameQuery.Animation({imageURL: "minion_explode.png", numberOfFrame: 11, delta: 52, rate: 30, type: $.gameQuery.ANIMATION_VERTICAL | $.gameQuery.ANIMATION_CALLBACK});

// Initialize the background
$.playground()
	.addGroup("background", {width: PLAYGROUND_WIDTH, height: PLAYGROUND_HEIGHT})
		.addSprite("background", {animation: background, width: PLAYGROUND_WIDTH, height: PLAYGROUND_HEIGHT})
		.addSprite("wall", {animation: wall, width: 884, height: 196, posx: 100,  posy: PLAYGROUND_HEIGHT - 250})
	.end()
	.addGroup("actors", {width: PLAYGROUND_WIDTH, height: PLAYGROUND_HEIGHT})
		.addGroup("player", {posx: PLAYGROUND_WIDTH/2, posy: PLAYGROUND_HEIGHT - 150, width: 96, height: 136})
			.addSprite("playerBody",{animation: playerAnimation["down"], posx: 0, posy: 0, width: 96, height: 136})
			//.addSprite("playerUp",{animation: playerAnimation["up"], posx: 0, posy: 0, width: 96, height: 136})
		.end();

$("#player")[0].player = new Player ($("#player"));
$("#playername").html("Player");

//This function manage the creation of the enemies
$.playground().registerCallback(function(){
	if(!gameOver)
	{
		if (!enemySpawn)
		{
			var name = "enemy1_"+Math.ceil(Math.random()*1000);
			var posX = (Math.random()*PLAYGROUND_WIDTH) - 50;
			var posY = (Math.random()*PLAYGROUND_HEIGHT) - 50;

			if (posX < 100)
				posX = 100;
			else if (posX > PLAYGROUND_WIDTH - 50 - 100)
				posX = PLAYGROUND_WIDTH - 50 - 100;

			if (posY < 30)
				posY = 30;
			else if (posY > PLAYGROUND_HEIGHT -50 - 175)
				posY = PLAYGROUND_HEIGHT -50 - 175;
			$("#actors").addSprite(name, {animation: enemies[0]["idle"], posx: posX, posy: posY,width: 50, height: 50});
			$("#"+name).addClass("enemy");
			$("#"+name)[0].enemy = new TargetDisk($("#"+name));
			$("#"+name).click (function (e) {
				if (isPlayerStaying){
					if (this.enemy.damage (e))
					{
						$("#"+name).remove ();
						enemySpawn = false;
					}
				}
			});

			enemySpawn = true;
		}
	}
}, 1000); //once per seconds is enough for this

$.playground().registerCallback(function(){
	if(!gameIsRunning)
		return;
	var remainTime = ((gameTime + CONS_GAME_TIME) - new Date().getTime()) / 1000;
	var remainTimeString = "" + remainTime;
	
	if(remainTime <= 0){
		gameOver = true;
		gameIsRunning = false;
		remainTimeString = "0.00";
		if(playerScore >= "2500"){
			show_message("Geschafft!", "Sie können ins nächste Level!" , "3");
		}else{
			show_message("Leider nicht geschafft!", "Versuchen Sie es nochmal!<br>Sie brauchen mindestens 2500 Punkte!" , "3");
		};
//		setTimeout(function(){
//			clearGame();
//			loadLevel("level_2"); 
//		}, 5000);
	}
	$("#hub #time").html(remainTimeString.slice(0, remainTimeString.indexOf(".") + 3));
	$("#hub #points").html("Points: " + playerScore);
}, 100);